/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package xin.alum.aim.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.AbstractReferenceCounted;
import io.netty.util.ReferenceCounted;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import lombok.Data;
import lombok.EqualsAndHashCode;
import xin.alum.aim.Aim;
import xin.alum.aim.util.GZipUtil;

/**
 * 需要向另一端发送的结构体
 */
@EqualsAndHashCode(callSuper = true)
@Data
public abstract class Transportable extends AbstractReferenceCounted {

    @JsonIgnore
    protected final InternalLogger logger = InternalLoggerFactory.getInstance(this.getClass());
    /**
     * JSON编码存储，防止重复编码，影响性能
     */
    @JsonIgnore
    protected String json;
    /**
     * Body字节编码存储，防止重复编码，影响性能
     */
    @JsonIgnore
    protected byte[] body;
    /**
     * 消息优先级,默认为0，值越大优先级越高
     */
    private Integer priority = 0;
    /**
     * 消息优先级,默认为0，值越大优先级越高
     */
    private String recipient;
    /**
     * 时间戳
     */
    private Long timestamp= System.currentTimeMillis();

    /**
     * 初始化参数
     */
    public Transportable() {
        super();
    }

    /**
     * 消息体字节数组
     */
    public abstract byte[] toBytes();

    /**
     * Tlv类型中的Tag---请自定义
     */
    public abstract int getTag();

    /**
     * 获取对象ByteBuf
     */
    @JsonIgnore
    public ByteBuf toByteBuf() {
        if (body == null) {
            body = this.toBytes();
            body = GZipUtil.gzip(Aim.properties.getGzip(), body);
        }
        return Unpooled.copiedBuffer(body);
    }

    /**
     * 获取 This的 JSON串
     *
     */
    @JsonIgnore
    public String toJson() {
        try {
            if (json == null) {
                json = this.toString();
                json = GZipUtil.gzip(Aim.properties.getGzip(), json);
            }
        } catch (Exception e) {
            logger.error(e);
        }
        return json;
    }

    @Override
    protected void deallocate() {
        this.json = null;
        this.body = null;
    }

    @Override
    public ReferenceCounted touch(Object hint) {
        return this;
    }
}
